/**
 * Created by wanghaun on 2016/7/6.
 */

define(function(require,exports,module){
    require("commonStaticDirectory/plugins/feedback/feedback.css");
    require("commonStaticDirectory/plugins/artDialog/ui-dialog.css");
    require("commonStaticDirectory/plugins/artDialog/dialog-plus");

    var Feedback = function (options) {
        this.init(options);
    };
    Feedback.prototype={
        constructor:Feedback,
        oDefaultOptions:{
            title:'殿下，终于等到您，快来给我们的系统点个赞吧',
            listData:[
                {
                    key:'interface',
                    name:"界面美观",
                    value:1
                },
                {
                    key:'product',
                    name:'产品易用',
                    value:2
                },
                {
                    key:'function',
                    name:'功能完善',
                    value:3
                },
                {
                    key:'data',
                    name:'数据准确',
                    value:4
                }
            ]
        },
        init:function (options) {
            this.options = $.extend(true, {}, this.oDefaultOptions, options);
            this._initDialog(this.options);
            this.$feedback=$("#feedbackPanel");
            this.bindEvt();
        },
        /**
         * 初始化弹出框
         * @param options
         * @private
         */
        _initDialog:function (options) {
            var t = this;
            var sTableHtml=options.listData.map(function(item){
                var str = '<tr><td class="key" data-type="'+item.key+'">'+item.name+'</td><td class="value">';
                for(var i=1;i<=5;i++){
                    str+= i<=item.value?'<span class="item active"></span>':'<span class="item"></span>';
                }
                str+='</td></tr>';
                return str;
            }).join("");

            var d = dialog({
                title:'我要反馈',
               /* width:648,
                height:325,*/
                content:
                '<div id="feedbackPanel" class="feedback-panel">'
                +   '<div class="feedback-title">'+options.title+'</div>'
                +   '<div class="feedback-content">'
                +       '<table>'+sTableHtml+'</table>'
                +       '<textarea class="feedback-input" cols="5" rows="4" placeholder="殿下，有什么想说的，在这里填写吧"></textarea>'
                +   '</div>'
                +'</div>',
                fixed: true,
                id: 'feedbackDialog',
                ok: function () {
                    var data=t.getCommitData();
                    /**
                     * 提交按钮...还未做
                     */
                },
                okValue: '提交'
            });
            d.showModal();
        },
        /**
         * 绑定事件
         */
        bindEvt:function () {
            this.$feedback.on("click",".item",function () {
                var $value=$(this).closest(".value");
                var index=$value.find(".item").index($(this));
                $value.find(".item").filter(":gt("+index+")").removeClass("active");
                $value.find(".item").filter(":lt("+(index+1)+")").addClass("active");
            })
        },
        /**’
         * 获取将要提交的数据
         * @returns {{}}
         */
        getCommitData:function () {
            var oData={};
            this.$feedback.find("tr").each(function () {
                var sType=$(this).find(".key").attr("data-type");
                var iValue=$(this).find(".value .item").filter(".active").length;
                oData[sType]=iValue;
            });
            //输入框建议--suggestion数据
            oData["suggestion"]=this.$feedback.find(".feedback-input").val();
            console.log(oData);
            return oData;
        }
    };
    module.exports = Feedback;
});